package com.catmiao.rdd.operate.transform;

import com.google.common.collect.Lists;
import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author ChengMiao
 * @title: Transfer_01_Map
 * @projectName spark_study
 * @description: TODO
 * @date 2024/11/25 16:27
 */
public class Transfer_05_FlatMap {

    public static void main(String[] args) {

        final SparkConf conf = new SparkConf();
        conf.setAppName("appName");
        conf.setMaster("local[*]");

        final JavaSparkContext jsc = new JavaSparkContext(conf);


        ArrayList<ArrayList<Integer>> list = Lists.newArrayList(
                Lists.newArrayList(1, 2),
                Lists.newArrayList(3, 4),
                Lists.newArrayList(5, 6)
        );


        JavaRDD<ArrayList<Integer>> rdd = jsc.parallelize(list, 2);

        JavaRDD<Integer> rdd2 = rdd.flatMap(new FlatMapFunction<ArrayList<Integer>, Integer>() {
            @Override
            public Iterator<Integer> call(ArrayList<Integer> iter) throws Exception {

                List<Integer> result = new ArrayList<>();
                for (Integer i : iter) {
                    result.add(i * 2);
                }
                return result.iterator();
            }
        });

        rdd2.collect().forEach(System.out::println);


        jsc.close();
    }
}
